package com.tfjybj.itoo.teach.provider.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.SemesterEntity;
import com.tfjybj.itoo.teach.model.SemesterModel;
import com.tfjybj.itoo.teach.provider.service.SemesterService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * SemesterController
 * semester表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-14 14:41:55
 */
@Api(tags = {"semester表接口"})
@RequestMapping(value = "/semester")
@RestController
public class SemesterController {

    @Resource
    private SemesterService semesterService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model SemesterModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody SemesterModel model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        SemesterEntity semesterEntity = new SemesterEntity();
        BeanUtils.copyProperties(model, semesterEntity);
        semesterService.save(semesterEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        semesterService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        semesterService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model SemesterModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "根据id修改semester")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody SemesterEntity model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        SemesterEntity semesterEntity = new SemesterEntity();
        BeanUtils.copyProperties(model, semesterEntity);
        semesterService.updateById(semesterEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Semester
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        SemesterEntity semesterEntity = semesterService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterEntity);
    }

    /**
     * 分页查询所有Semester
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "分页查询所有Semester")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<SemesterEntity> semesters = semesterService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesters);
    }
	
	/**
     * 根据学年学期名称模糊查询semester
     *
     * @param name  学年学期名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的semester
     * @author 田成荣
     * @since 2.0.0 2018-11-14 14:41:55
     */
    @ApiOperation(value = "根据学年学期名称模糊查询semester", notes = "分页根据学年学期名称模糊查询semester")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "querySemesterByLikeName/{pageNo}/{pageSize}")
    public ItooResult querySemesterByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                              @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<SemesterEntity> semesterList = semesterService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 获得学年学期代码(包含本学期)- 井朝 2018年11月14日18:52:48
     * getYearSemesterCode(-1,1),获取结果（201620172,201720181,201720182）.
     *
     * @param startSemester 当前学年学期为0，从当前学年学期开始想获取的之前学年学期的数量
     * @param endSemester   当前学年学期为0，从当前学年学期开始想获取的之后学年学期的数量
     * @return List<String> 学年学期代码list对象
     */

    /**
     * 根据学年学期名称模糊查询semester
     *
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 查询所有SemesterEntity记录
     * @author 胡泽宽
     * @since 2.0.0 2018年11月17日 15:32:59
     */
    @ApiOperation(value = "查询所有SemesterEntity记录", notes = "查询所有SemesterEntity记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "querySemester/{pageNo}/{pageSize}")
    public ItooResult querySemester(
            @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<SemesterEntity> semesterList = semesterService.querySemester(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterList);
    }

    /**
     * 添加
     *
     * @param model SemesterModel
     * @return 添加的结果
     * @author 胡泽宽
     * @since 2.0.0 2018年11月20日 11:38:44
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/createsemester"})
    public ItooResult createsemester(@RequestBody SemesterEntity model) {
        if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        //重复添加校验
        Integer flagcount= semesterService.checkSemesterbycodeorname(model.getName(),model.getCode());
        if(flagcount>0){
            return ItooResult.build(ItooResult.SUCCESS, "本条数据已添加");
        }
        SemesterEntity semesterEntity = new SemesterEntity();
        BeanUtils.copyProperties(model, semesterEntity);
        semesterEntity.setId(IdWorker.getIdStr());
        semesterService.save(semesterEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 根据学年学期名称或code模糊查询semester
     *
     * @param name  学年学期名称或code
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的semester
     * @author 胡泽宽
     * @since 2.0.0 2018年11月21日 10:15:57
     */
    @ApiOperation(value = "根据学年学期名称或code模糊查询semester", notes = "分页根据学年学期名称模糊查询semester")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryLikeNameorCode/{pageNo}/{pageSize}")
    public ItooResult queryLikeNameorCode(@RequestParam(required = false, defaultValue = "") String name,
                                          @PathVariable Integer pageNo, @PathVariable Integer pageSize) {

        PageInfo<SemesterEntity> semesterList = semesterService.queryLikeNameorCode(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterList);
    }

    /**
     * 详细根据学年学期名称或code模糊查询semester
     * @param pageNo   页码
     * @param pageSize 页数
     * @param name  学年学期名称
     * @param isGraduate  是否毕业
     * @param isCurrent  是否当前
     * @return 模糊查询的semester
     * @author 胡泽宽
     * @since 2.0.0 2018年11月21日 15:17:24
     */
    @ApiOperation(value = "详细根据学年学期名称或code模糊查询semester")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = {"/queryLikeNameorCodedetail/{pageNo}/{pageSize}"})
    public ItooResult queryLikeNameorCodedetail(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false, defaultValue = "") Integer isGraduate, @RequestParam(required = false, defaultValue = "") Integer isCurrent, @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<SemesterEntity> semesterList =semesterService.queryLikeNameorCodedetail(name,isGraduate,isCurrent,pageNo,pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterList);
    }

    /**
     * 获得当前学年学期的前后的几条记录的详细信息
     * @return List<SemesterEntity>
     * @author 胡泽宽
     * @since 2.0.0 2018年11月24日 10:21:53
     */
    @ApiOperation(value = "获得当前学年学期的前后的几条记录的详细信息")
    @GetMapping(value = {"/getYearSemesterList/{startSemester}/{endSemester}"})
    public ItooResult getYearSemesterList(@ApiParam(name = "startSemester",value = "起始数",required = true,example = "1")@PathVariable Integer startSemester,
                                          @ApiParam(name = "endSemester",value = "结束数",required = true,example = "10")@PathVariable Integer endSemester) {
        List<SemesterEntity> semesters = semesterService.getYearSemesterList(startSemester, endSemester);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesters);
    }

    /**
     * 获得当前学年学期的前后的几条记录的详细信息map
     * @return LinkedHashMap<String, String>
     * @author 胡泽宽
     * @since 2.0.0 2018年11月24日 11:01:25
     */
    @ApiOperation(value = "获得当前学年学期的前后的几条记录的详细信息")
    @GetMapping(value = {"/getYearSemesterMap/{startSemester}/{endSemester}"})
    public ItooResult getYearSemesterMap(@ApiParam(name = "startSemester",value = "起始数",required = true,example = "1")@PathVariable Integer startSemester,
                                         @ApiParam(name = "endSemester",value = "结束数",required = true,example = "10")@PathVariable Integer endSemester) {
        LinkedHashMap<String, String> semesters = semesterService.getYearSemesterMap(startSemester, endSemester);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesters);
    }

    /**
     * 获得当前学年学期code的前后的几条记录list<string>
     * @return List<String>
     * @author 胡泽宽
     * @since 2.0.0 2018年11月24日 11:01:25
     */
    @ApiOperation(value = "获得当前学年学期code的前后的几条记录list<string>")
    @GetMapping(value = {"/getYearSemesterCode/{startSemester}/{endSemester}"})
    public ItooResult getYearSemesterCode(@ApiParam(name = "startSemester",value = "起始数",required = true,example = "1")@PathVariable Integer startSemester,
                                          @ApiParam(name = "endSemester",value = "结束数",required = true,example = "10")@PathVariable Integer endSemester) {
         List<String> semesters = semesterService.getYearSemesterCode(startSemester, endSemester);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesters);
    }

    /**
     * 获得当前学年学期name的前后的几条记录list<string>
     * @return List<String>
     * @author 胡泽宽
     * @since 2.0.0 2018年11月24日 11:01:25
     */
    @ApiOperation(value = "获得当前学年学期name的前后的几条记录list<string>")
    @GetMapping(value = {"/getYearSemesterName/{startSemester}/{endSemester}"})
    public ItooResult getYearSemesterName(@ApiParam(name = "startSemester",value = "起始数",required = true,example = "1")@PathVariable Integer startSemester,
                                          @ApiParam(name = "endSemester",value = "结束数",required = true,example = "10")@PathVariable Integer endSemester) {
        List<String> semesters = semesterService.getYearSemesterName(startSemester, endSemester);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesters);
    }

    /**
     * 根据searchContent导出学年学期
     *
     * @param response 响应
     * @param searchContent 模糊查询的信息
     * @return 导出结果信息
     * @author 胡泽宽
     * @since 2.0.0 2018年11月25日 11:03:55
     */
    @ApiOperation(value = " 根据searchContent导出学年学期")
    @GetMapping(value = {"/exportByNameorCode"})
    public ItooResult exportByNameorCode(HttpServletResponse response,
                                         @RequestParam(required = false, defaultValue = "") String searchContent) {
        return semesterService.exportByNameorCode(response, searchContent);

    }
    /**
     * 导入学年学期下载模板
     *
     * @param response 响应
     * @param searchContent 模糊查询的信息
     * @return 导出提供下载的模板
     * @author 胡泽宽
     * @since 2.0.0 2018年11月27日 14:33:32
     */
    @ApiOperation(value = "导入学年学期下载模板")
    @GetMapping(value = {"/exportTemplate"})
    public ItooResult exportTemplate(HttpServletResponse response,
                                     @RequestParam(required = false, defaultValue = "") String searchContent) {
        return semesterService.exportTemplate(response, searchContent);

    }

    /**
     * 批量导入学年学期
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 胡泽宽
     * @since 2.0.0 2018年11月27日 15:56:29
     */
    @ApiOperation(value = "批量导入学年学期", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importsemesterByTemplate"})
    public ItooResult importsemesterByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                               HttpServletRequest request, HttpServletResponse response) {
        return semesterService.importsemesterByTemplate(multipartFile, request, response);
    }

    /**
     * 获取下一年的学年学期
     * @author 井朝
     * @since 2018年12月19日13:18:12S
     */
    @ApiOperation(value = "获取下一年的学年学期")
    @GetMapping(value = {"/getNextYearSemester"})
    public ItooResult getNextYearSemester(){
        List<SemesterEntity> semesterEntities = semesterService.getNextYearSemester();
        return ItooResult.build(ItooResult.SUCCESS, "查询完成", semesterEntities);
    }

}    
